<script setup lang="ts" xmlns="http://www.w3.org/1999/html">
  import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
  import {
    reqDictDataDeleteUrl,
    reqDictDataPageListUrl,
  } from '@/api/system/dict/data.ts'
  import DictTag from '@/components/DictTags/index.vue'
  import { useRoute } from 'vue-router'
  import { getDictTypeAllUrl, reqDictTypeInfoUrl } from '@/api/system/dict'
  import DataDialog from './components/dataDialog.vue'

  const { proxy }: any = getCurrentInstance()
  const $route = useRoute()

  //总的页面数
  const total = ref()
  const dictId = ref()
  //分页查询条件
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 5,
    dictType: undefined,
    dictLabel: undefined,
    status: undefined,
  })

  // 非单个禁用---判断修改按钮是否可用
  const single = ref(true)
  // 非多个禁用---判断删除按钮
  const multiple = ref(true)
  // 多选框选中数据
  const codes = ref([]) // 非多个禁用
  const showSearch = ref(true)
  const dictTypeOptios = ref([])
  //遮罩层
  const loading = ref(true)
  //要展示的数据
  const dataList = ref([])
  const dataDialog = ref() /*对话框ref*/

  //重置
  const ruleFormRef = ref()
  const resetQuery = async () => {
    await proxy.$resetForm(ruleFormRef)
    await getTypeByDictId(dictId.value).then(async () => {
      loading.value = true
      await getDictDataPageList()
    })
  }

  //生命周期函数
  onMounted(async () => {
    //获取第一页的信息
    const dict_id = $route.params && $route.params.dictId
    console.log('dict_id', dict_id)
    dictId.value = dict_id
    /*获取所有的字典类型数据*/
    await getDictTypeAllUrl().then((req: any) => {
      dictTypeOptios.value = req.data
    })
    await getTypeByDictId(dictId.value).then(async () => {
      loading.value = true
      await getDictDataPageList()
    })
  })

  /** 查询字典类型详细 */
  const getTypeByDictId = async (dictId: any) => {
    await reqDictTypeInfoUrl(dictId).then((req: any) => {
      if (req.code === 200) {
        queryParams.dictType = req.data.dictType
      }
    })
  }

  /*进行分页查询*/
  const getDictDataPageList = async () => {
    await reqDictDataPageListUrl(queryParams).then((req: any) => {
      if (req.code == 200) {
        dataList.value = req.data
        total.value = req.total
        loading.value = false
      } else {
        proxy.$modal.msgError(req.message)
      }
    })
  }

  //点击弹出添加或者修改对话框
  /*打开对话框进行添加操作*/
  const addHandler = () => {
    dataDialog.value.show({ dictType: queryParams.dictType })
  }

  /*修改操作*/
  const editHandler = (row: any = {}) => {
    dataDialog.value.show(row)
  }

  /*多选框*/
  const handleSelectionChange = (selection: any) => {
    codes.value = selection.map((item: any) => item.dictCode)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  /*删除加上批量删除*/
  const handleDelete = (row: any = {}) => {
    const dictCodes = row.dictCode || codes.value
    proxy.$modal
      .confirm('是否确认删除岗位编号为"' + dictCodes + '"的数据项？')
      .then(async () => {
        await reqDictDataDeleteUrl(dictCodes).then(async (req: any) => {
          if (req.code == 200) {
            await getDictDataPageList()
            proxy.$modal.msgSuccess('删除成功')
          } else {
            proxy.$modal.msgError(req.message != null ? req.message : '删除成功')
          }
        })
      })
      .catch(() => {})
  }
</script>

<script lang="ts">
  export default {
    name: 'Data',
    dicts: ['sys_normal_disable'],
  }
</script>
<template>
  <div class="app-container">
    <div class="header">
      <el-form
        class="demo-form-inline"
        :model="queryParams"
        ref="ruleFormRef"
        size="default"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="字典名称" prop="dictType">
          <el-select v-model="queryParams.dictType">
            <el-option
              v-for="item in dictTypeOptios"
              :key="item.dictId"
              :label="item.dictName"
              :value="item.dictType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="queryParams.dictType" disabled />
        </el-form-item>

        <el-form-item label="字典标签" prop="dictLabel">
          <el-input
            v-model="queryParams.dictLabel"
            placeholder="请输入字典标签"
            clearable
            @keyup.enter.native="getDictDataPageList"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="数据状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" size="default" @click="getDictDataPageList">
            搜索
          </el-button>
          <el-button icon="Refresh" size="default" @click="resetQuery"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <div class="util">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="default"
          @click="addHandler"
          v-hasPermi="['system:dict:add']"
        >
          新增
        </el-button>
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="default"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dict:remove']"
        >
          批量删除
        </el-button>
        <el-button
          type="warning"
          plain
          icon="Download"
          size="default"
          @click="handleExport"
          v-hasPermi="['system:dict:export']"
        >
          导出
        </el-button>
        <el-button type="warning" plain icon="Close" size="default" @click="handleClose">
          关闭
        </el-button>
      </div>
      <!--表格    -->
      <div class="table-inner">
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="字典编码" align="center" prop="dictCode" />
          <el-table-column label="字典标签" align="center" prop="dictLabel">
            <template #default="scope">
              <span v-if="scope.row.listClass == '' || scope.row.listClass == 'default'">
                {{ scope.row.dictLabel }}
              </span>
              <el-tag v-else :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass">
                {{ scope.row.dictLabel }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="字典键值" align="center" prop="dictValue" />
          <el-table-column label="字典排序" align="center" prop="dictSort" />
          <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <DictTag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          ></el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button
                size="default"
                type="text"
                icon="el-icon-edit"
                @click="editHandler(scope.row)"
                v-hasPermi="['system:dict:edit']"
              >
                修改
              </el-button>
              <el-button
                size="default"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:dict:remove']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页器            -->
      <div class="pagination">
        <el-pagination
          style="margin-top: 40px"
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          :background="true"
          layout="prev, pager, next, jumper, ->,  sizes, total"
          :total="total"
          @current-change="getDictDataPageList"
          @size-change="getDictDataPageList"
        />
      </div>
    </div>
    <DataDialog ref="dataDialog" @getDictDataPageList="getDictDataPageList"></DataDialog>
  </div>
</template>

<style scoped lang="scss">
  .header {
    display: flex;
    padding: 16px 16px 0px 16px;
    margin-bottom: 16px;
    border-radius: 4px;
    background: white;
    box-shadow: 0 0 12px rgb(0 0 0 / 5%);
  }

  .footer {
    flex: 1;
    display: flex;
    padding: 16px;
    flex-direction: column;
    border-radius: 4px;
    overflow: hidden;
    background: white;
    box-shadow: 0 0 12px rgb(0 0 0 / 5%);
    position: relative;
    box-sizing: border-box;

    .util {
      margin-bottom: 15px;
      display: flex;
      justify-content: flex-end;
      flex-shrink: 0;
    }

    .table-inner {
      flex: 1;
      position: relative;
    }

    .table {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
    }
  }
</style>
